const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const ChapterHighlights = sequelize.define('ChapterHighlights', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  chapterNumber: {
    type: DataTypes.INTEGER,
    allowNull: false,
    unique: true,
    comment: '章节号'
  },
  title: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: '章节标题，如：第1章 学习要点'
  },
  // 左侧区域配置
  leftSection: {
    type: DataTypes.JSON,
    allowNull: false,
    comment: '左侧区域配置，包含标题、图标和子节',
    defaultValue: {
      title: '',
      icon: 'lightning',
      subsections: []
    }
  },
  // 右侧区域配置
  rightSection: {
    type: DataTypes.JSON,
    allowNull: false,
    comment: '右侧区域配置，包含标题、图标和子节',
    defaultValue: {
      title: '',
      icon: 'book',
      subsections: []
    }
  },
  // 提示信息
  tip: {
    type: DataTypes.JSON,
    comment: '学习建议提示信息',
    defaultValue: {
      label: '学习建议',
      content: ''
    }
  },
  // 是否启用
  isActive: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: '是否启用显示'
  },
  // 排序权重
  sortOrder: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '排序权重，数值越小越靠前'
  },
  // 创建者
  createdBy: {
    type: DataTypes.INTEGER,
    comment: '创建者用户ID'
  },
  // 更新者
  updatedBy: {
    type: DataTypes.INTEGER,
    comment: '更新者用户ID'
  }
}, {
  tableName: 'chapter_highlights',
  timestamps: true,
  indexes: [
    {
      unique: true,
      fields: ['chapterNumber']
    },
    {
      fields: ['isActive']
    },
    {
      fields: ['sortOrder']
    }
  ]
});

// 定义子节数据结构的验证规则
ChapterHighlights.validateSubsection = (subsection) => {
  const requiredFields = ['title', 'color', 'type', 'items'];
  const validTypes = ['list', 'stages', 'path', 'percentage', 'comparison', 'modifiers'];
  
  // 检查必需字段
  for (const field of requiredFields) {
    if (!subsection.hasOwnProperty(field)) {
      throw new Error(`子节缺少必需字段: ${field}`);
    }
  }
  
  // 检查类型是否有效
  if (!validTypes.includes(subsection.type)) {
    throw new Error(`无效的子节类型: ${subsection.type}，有效类型: ${validTypes.join(', ')}`);
  }
  
  // 检查items是否为数组
  if (!Array.isArray(subsection.items)) {
    throw new Error('子节的items必须是数组');
  }
  
  return true;
};

// 定义钩子函数，在保存前验证数据结构
ChapterHighlights.beforeCreate((instance) => {
  // 验证左侧区域的子节
  if (instance.leftSection && instance.leftSection.subsections) {
    instance.leftSection.subsections.forEach(subsection => {
      ChapterHighlights.validateSubsection(subsection);
    });
  }
  
  // 验证右侧区域的子节
  if (instance.rightSection && instance.rightSection.subsections) {
    instance.rightSection.subsections.forEach(subsection => {
      ChapterHighlights.validateSubsection(subsection);
    });
  }
});

ChapterHighlights.beforeUpdate((instance) => {
  // 验证左侧区域的子节
  if (instance.leftSection && instance.leftSection.subsections) {
    instance.leftSection.subsections.forEach(subsection => {
      ChapterHighlights.validateSubsection(subsection);
    });
  }
  
  // 验证右侧区域的子节
  if (instance.rightSection && instance.rightSection.subsections) {
    instance.rightSection.subsections.forEach(subsection => {
      ChapterHighlights.validateSubsection(subsection);
    });
  }
});

module.exports = ChapterHighlights;